<template>
  <div>
    <el-card style="width: 50%">
      <el-form :model="user" label-width="80px" style="padding-right: 50px" :rules="rules" ref="userForm">
        <div style="margin-bottom: 10px;text-align: center">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:9090/file/upload"
              :headers="{token: user.token}"
              :show-file-list="false"
              :on-success="handAvatarSuccess"
          >
            <img v-if="user.avatar" :src="user.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="user.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="user.name"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="user.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="user.email"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input type="textarea" v-model="user.address"></el-input>
        </el-form-item>
        <div style="text-align: center">
          <el-button @click="update">保存</el-button>
        </div>
      </el-form>
    </el-card>
  </div>

</template>

<script>
import request from "@/utils/request";

export default {
  name: "Person",
  data() {
    return {
      user: JSON.parse(localStorage.getItem('base-user') || '{}'),
      rules: {
        name: [
          {required: true, message: '姓名输入格式有误', trigger: 'blur'},
          {min: 2, max: 5, message: '姓名长度应在2到5个字符之间', trigger: 'blur'},
          {pattern: /^[\u4e00-\u9fa5]+$/, message: '请输入有效的中文姓名', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '电话号码输入格式有误', trigger: 'blur'},
          {pattern: /^1[3-9]\d{9}$/, message: '请输入有效的手机号', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '邮箱输入格式有误', trigger: 'blur'},
          {type: 'email', message: '请输入有效的邮箱地址', trigger: ['blur', 'change']}
        ],
        address: [
          {required: true, message: '地址输入格式有误', trigger: 'blur'},
          {min: 5, message: '地址长度不能少于5个字符', trigger: 'blur'}
        ],
      }
    }
  },
  created() {
  },
  methods: {
    update() {
      this.$refs.userForm.validate((valid) => {
        if (valid) {
          //保存当前用户数据到数据库
          this.$request.put('/user/update', this.user).then(res => {
            if (res.code === '200') {
              //成功更新
              this.$message.success("更新成功")
              //更新浏览器缓存里的用户信息
              localStorage.setItem('base-user', JSON.stringify(this.user))
              //触发父级的实时更新
              this.$emit('update:user', this.user)
            } else {
              this.$message.error(res.msg)
            }
          })
        }else {
          this.$message.error('表单验证失败，不执行更新操作')
        }})
    },
    handAvatarSuccess(response, file, fileList) {
      this.user.avatar = response.data
    }
  }
}
</script>

<style scoped>
/deep/ .el-form-item__label {
  font-weight: bold;
}

/deep/ .el-upload {
  border-radius: 50%;
}

/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  border-radius: 50%;
}

/deep/ .avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
  border-radius: 50%;
}
</style>